package idiolect.helpers;

import idiolect.core.Constants;
import idiolect.core.IOUtil;

import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;

public class MultitranTranscriptionHelper extends AbstractHelper {
    private static final int[][] MULTITRAN_WEB_MAP = new int[][]{
            {34, 716}, {39, 712}, {58, 720}, {65, 652}, {68, 240}, {69, 604}, {73, 618}, {78, 331},
            {79, 596}, {80, 594}, {81, 593}, {83, 643}, {84, 952}, {86, 650}, {90, 658}, {113, 601}, {120, 230}};

    private static final String STARTMARKER = "/gif/[.gif";
    private static final String STOPMARKER = "/gif/].gif";
    private static final String SYMBOL_MARKER = "/gif/";


    public MultitranTranscriptionHelper(String request, ResponseAcceptor requestor, int taskID) {
        super(request, requestor, taskID);
    }

    public void run() {
        try {
            String page = new String(IOUtil.getContent(new URL("http://www.multitran.ru/c/m.exe?l1=1&l2=2&s=" + URLEncoder.encode(request))));
            int startPos = page.indexOf(MultitranTranscriptionHelper.STARTMARKER);
            if (startPos == -1)
                throw new IOException("Transcription not found for '" + request + "'");
            int beforePos = page.lastIndexOf("<a", startPos);
            if (beforePos == -1)
                throw new IOException("Transcription not found for '" + request + "'");
            beforePos = page.indexOf(">", beforePos);
            if (beforePos == -1)
                throw new IOException("Transcription not found for '" + request + "'");
            int endWordPos = page.indexOf("</a>", beforePos);
            if (endWordPos == -1)
                throw new IOException("Transcription not found for '" + request + "'");
            if (!page.substring(beforePos + 1, endWordPos).equals(request))
                throw new IOException("Transcription not found for '" + request + "'");
            int stopPos = page.indexOf(MultitranTranscriptionHelper.STOPMARKER, startPos + MultitranTranscriptionHelper.STARTMARKER.length());
            if (stopPos == -1)
                throw new IOException("Transcription not found for '" + request + "'");
            String transcriptionFragment = page.substring(startPos + MultitranTranscriptionHelper.STARTMARKER.length(), stopPos);
            StringBuilder result = new StringBuilder();
            boolean found;
            int p = 0;
            int pp;
            do {
                found = false;
                p = transcriptionFragment.indexOf(SYMBOL_MARKER, p);
                if (p != -1) {
                    p += 5;
                    pp = transcriptionFragment.indexOf('.', p);
                    if (found = pp != -1) {
                        result.append(MultitranTranscriptionHelper.getChar(transcriptionFragment.substring(p, pp)));
                        if (result.length() > 1 &&
                                result.charAt(result.length() - 1) == Constants.TOTAL.charAt(40) &&
                                result.charAt(result.length() - 2) == Constants.TOTAL.charAt(18)) {
                            result.delete(result.length() - 2, result.length());
                            result.append(Constants.TOTAL.charAt(20));
                        }
                    }
                }
            } while (found);
            response = result.toString();
        } catch (IOException e) {
//            e.printStackTrace();
            response = null;
        }
    }

    private static char getChar(String mtImgName) {
        try {
            int num = Integer.parseInt(mtImgName);
            for (int[] pair : MULTITRAN_WEB_MAP) {
                if (pair[0] == num)
                    return (char) pair[1];
            }
            return (char) num;
        } catch (NumberFormatException e) {
            return 0;
        }
    }
}
